package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.BankAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.helper.ImageLoader;
import com.pengcheng.taskapplication.model.BankVo;
import com.pengcheng.taskapplication.model.OpenAccountResult;
import com.pengcheng.taskapplication.model.WalletModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.ToastUtils;

import org.w3c.dom.Text;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZFT on 2020/10/27.
 */

public class MyBankList extends BaseActivity implements IView{
    private UserPresenter userPresenter;
    @BindView(R.id.pull_to_refresh_lv)
    PullToRefreshListView pullToRefreshListView;
    @BindView(R.id.img_show)
    ImageView img_show;
    @BindView(R.id.tv_center_title)
    TextView tv_center;

    private String token;



    ArrayList<WalletModel> list =new ArrayList<>();
    private BankAdapter adapter;
    @Override
    protected int layoutResID() {
        return R.layout.activity_my_bank_list_layout;
    }

    @Override
    protected void initData() {
        tv_center.setText("我的钱包");
        userPresenter = new UserPresenter(this);

        //adapter
        adapter = new BankAdapter(context,list);
        //manager
        pullToRefreshListView.setAdapter(adapter);

        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
//                pageIndex=1;
//                list.clear();
//                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//                pageIndex++;
//                getData();
            }
        });

        ImageLoader.displayImage(context,"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603966381533&di=3fba061070b9a73f336b7328819b2f84&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg",
                img_show);
        queryAccount();
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.queryAccount)){
            Gson gson= new Gson();
            Type type =new TypeToken<WalletModel>(){}.getType();
            WalletModel walletModel= gson.fromJson(gson.toJson(data),type);
            list.clear();
            list.add(walletModel);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        ToastUtils.showShortToast(msg);
    }

    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }
    public void queryAccount(){
        userPresenter.queryAccount(ConstantValue.RequestKey.queryAccount);
    }

}
